#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SIZE 10

void generate_random_walk(char board[SIZE][SIZE]) {
    int row = 0, col = 0;
    char letter = 'A';
    int directions[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // 上、下、左、右

    // 初始化棋盘
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            board[i][j] = '.';
        }
    }

    board[row][col] = letter++;

    // 生成随机路径
    while (letter <= 'Z') {
        int shuffled[4] = {0, 1, 2, 3}; // 方向索引
        // **随机打乱方向数组，确保所有方向都被尝试**
        for (int i = 3; i > 0; i--) {
            int j = rand() % (i + 1);
            int temp = shuffled[i];
            shuffled[i] = shuffled[j];
            shuffled[j] = temp;
        }

        int moved = 0;
        for (int i = 0; i < 4; i++) {
            int dir = shuffled[i];
            int new_row = row + directions[dir][0];
            int new_col = col + directions[dir][1];

            // 检查边界和是否已访问
            if (new_row >= 0 && new_row < SIZE && new_col >= 0 && new_col < SIZE && board[new_row][new_col] == '.') {
                row = new_row;
                col = new_col;
                board[row][col] = letter++;
                moved = 1; // 记录成功移动
                break;
            }
        }

        if (!moved) break; // 如果 4 个方向都走不通，则终止
    }
}

void print_board(char board[SIZE][SIZE]) {
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            printf("%c ", board[i][j]);
        }
        printf("\n");
    }
}

int main() {
    char board[SIZE][SIZE];

    srand(time(NULL)); // 初始化随机种子

    generate_random_walk(board);
    print_board(board);

    return 0;
}
